package com.axi.resplan.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import com.axi.ga.GaEngine;
import com.axi.ga.IDns;
import com.axi.ga.IGaListener;
import com.axi.resplan.*;

import javax.swing.*;

public class TaskPlanPanel extends JTable //JPanel
 implements IGaListener
{

	/** The Task that belongs to this view */
	com.axi.resplan.ResPlanDns _dns;
	String _dnsString;
	
	
	/** The Object that contains this task */
	TimeTableFrame _parent;
	
	
	TimeTable _tt;
	GaEngine _gae;
	ResPlanGa _rpg;

	Date _planStartDate;
	Date _planEndDate;
	Date _startDate;
	Date _endDate;
	
	
	//data for view
	int _zoom = 100;
	int _offsetX = 0;
	int _offsetY = 0;
	int _m=10;
	
	/**
	 * 
	 * @param argParent
	 */
	public TaskPlanPanel( TimeTableFrame argParent)
	{	
		//super();
		this(300, argParent.getTimeTable().getResourceVector().size()+1);
		zoomTable(20);	
		//setPreferredSize( new Dimension(300, 100));
		_parent = argParent;
		_tt = _parent.getTimeTable();
		_gae = _parent.getGaEngine();
		_gae.addGaEngineListener(this);//
		_rpg = (ResPlanGa)_gae.getTheDnsHandler();
		this._planStartDate = _tt.getStartDate();
		this._startDate = _tt.getEarliestDate();
		this._planEndDate = _tt.getExpectedEndDate();
		this._endDate = _tt.getLatestDate();
	}

	
	public TaskPlanPanel( int argSizeX, int argSizeY)
	{
		super(argSizeY, argSizeX);
	}
	
	
	public void zoomTable(int argCellWith)
	{
		setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		int maxi = this.getColumnModel().getColumnCount();
		for (int i = 0; i < maxi; i++) 
		{
		    this.getColumnModel().getColumn(i).setPreferredWidth( argCellWith );
		}
	}
	
	
	
	public void updateView()
	{		

		System.out.println("TaskPlanPanel > updateView");
		
		//clear old content
		resetView();

		//evaluate the DNS and keep the result for displaying
		Hashtable ht = _rpg.getTheBestDetails();
		
		if(ht==null)
			return;
		
		//find the planed start and planed end date
		
		//find real start and end date
		
		//get vectors with task grouped by resources
		Enumeration en;
		Vector resV = _tt.getResourceVector();
		int resNumMax = resV.size();
		int startPos=2;
		for(int i=0; i<resNumMax; i++)
		{
			
			//display the name of the resource at the begining of the given line
			setValueAt((String)resV.get(i), i, 0);
			//this.setValueAt(obj, y, x)
			
			//Display the content of a vector task by task
			//fill as many cells as many hours a task takes with the task id
			TaskVector tv = (TaskVector)ht.get( resV.get(i) );
			en = tv.elements();
			int pos=startPos;
			int endPos;
			while(en.hasMoreElements())
			{
				Task t = (Task)en.nextElement();
				endPos=pos+t.getLengthInHur()/8;
				//System.out.println("task:"+t.getTaskId()+", pos:"+pos+", endPos"+endPos+", length:"+(t.getLengthInHur()/8));
				for( ;pos<endPos;pos++)
				{
					setValueAt(t.getTaskId(), i, pos);
				}
			}
		}
		
		
		
		//repaint();		
	}

	
		
	private void resetView()
	{
		//use native JTable methodes
		this.selectAll();
		this.clearSelection();
	}		

	
	public void setTheBest( ResPlanDns argDns)
	{
		_dns = argDns;
		String _dnsString = _dns.toString();
		updateView();
	}

//IGaListener methodes
	
	
	public void terminated(IDns argTheBest)
	{
		
	}
	
	public void meetTheRequirement(IDns argTheBest)
	{
		
	}
	
	
	public void cycleEnd(IDns argTheBest)
	{
		System.out.println("TaskPlanPanel > cycleEnd");

		updateView();
		
		//pop up a modal widndow to ask for continue or stop
		
		//JOptionPane.showConfirmDialog
		int res = JOptionPane.showConfirmDialog(this, 
				"Continue GA?", 
				"GA running control",
				JOptionPane.OK_CANCEL_OPTION);
		
		if(res!=JOptionPane.OK_OPTION)
			this._gae.stopLife();
		
	}
	
	
	public void lifeStopped(IDns argTheBest)
	{
		
	}
	

	public void lifeStarted()
	{
		System.out.println("TaskPlanPanel > lifeStarted");

	}
	
	

}//end of class

